1   /*
2    * Copyright (C) 2007 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import com.google.common.annotations.Beta;
20  import com.google.common.annotations.GwtCompatible;
21  import com.google.common.base.Objects;
22  
23  import java.util.Map;
24  import java.util.Map.Entry;
25  
26  import javax.annotation.Nullable;
27  
28  /**
29   * A map entry which forwards all its method calls to another map entry.
30   * Subclasses should override one or more methods to modify the behavior of the
31   * backing map entry as desired per the <a
32   * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
33   *
34   * <p><i>Warning:</i> The methods of {@code ForwardingMapEntry} forward
35   * <i>indiscriminately</i> to the methods of the delegate. For example,
36   * overriding {@link #getValue} alone <i>will not</i> change the behavior of
37   * {@link #equals}, which can lead to unexpected behavior. In this case, you
38   * should override {@code equals} as well, either providing your own
39   * implementation, or delegating to the provided {@code standardEquals} method.
40   *
41   * <p>Each of the {@code standard} methods, where appropriate, use {@link
42   * Objects#equal} to test equality for both keys and values. This may not be
43   * the desired behavior for map implementations that use non-standard notions of
44   * key equality, such as the entry of a {@code SortedMap} whose comparator is
45   * not consistent with {@code equals}.
46   *
47   * <p>The {@code standard} methods are not guaranteed to be thread-safe, even
48   * when all of the methods that they depend on are thread-safe.
49   *
50   * @author Mike Bostock
51   * @author Louis Wasserman
52   * @since 2.0 (imported from Google Collections Library)
53   */
54  @GwtCompatible
55  public abstract class ForwardingMapEntry<K, V>
56      extends ForwardingObject implements Map.Entry<K, V> {
57    // TODO(user): identify places where thread safety is actually lost
58  
59    /** Constructor for use by subclasses. */
60    protected ForwardingMapEntry() {}
61  
62    @Override protected abstract Map.Entry<K, V> delegate();
63  
64    @Override
65    public K getKey() {
66      return delegate().getKey();
67    }
68  
69    @Override
70    public V getValue() {
71      return delegate().getValue();
72    }
73  
74    @Override
75    public V setValue(V value) {
76      return delegate().setValue(value);
77    }
78  
79    @Override public boolean equals(@Nullable Object object) {
80      return delegate().equals(object);
81    }
82  
83    @Override public int hashCode() {
84      return delegate().hashCode();
85    }
86  
87    /**
88     * A sensible definition of {@link #equals(Object)} in terms of {@link
89     * #getKey()} and {@link #getValue()}. If you override either of these
90     * methods, you may wish to override {@link #equals(Object)} to forward to
91     * this implementation.
92     *
93     * @since 7.0
94     */
95    protected boolean standardEquals(@Nullable Object object) {
96      if (object instanceof Entry) {
97        Entry<?, ?> that = (Entry<?, ?>) object;
98        return Objects.equal(this.getKey(), that.getKey())
99            && Objects.equal(this.getValue(), that.getValue());
100     }
101     return false;
102   }
103 
104   /**
105    * A sensible definition of {@link #hashCode()} in terms of {@link #getKey()}
106    * and {@link #getValue()}. If you override either of these methods, you may
107    * wish to override {@link #hashCode()} to forward to this implementation.
108    *
109    * @since 7.0
110    */
111   protected int standardHashCode() {
112     K k = getKey();
113     V v = getValue();
114     return ((k == null) ? 0 : k.hashCode()) ^ ((v == null) ? 0 : v.hashCode());
115   }
116 
117   /**
118    * A sensible definition of {@link #toString} in terms of {@link
119    * #getKey} and {@link #getValue}. If you override either of these
120    * methods, you may wish to override {@link #equals} to forward to this
121    * implementation.
122    *
123    * @since 7.0
124    */
125   @Beta protected String standardToString() {
126     return getKey() + "=" + getValue();
127   }
128 }